package com.yy.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.yy.common.constant.PmsConstants;
import com.yy.common.exception.ServiceException;
import com.yy.common.utils.DateUtils;
import com.yy.dto.ComplaintsDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yy.mapper.OaComplaintsMapper;
import com.yy.domain.OaComplaints;
import com.yy.service.IOaComplaintsService;

/**
 * 投诉工单Service业务层处理
 * 
 * @author yy
 * @date 2023-11-03
 */
@Service
public class OaComplaintsServiceImpl implements IOaComplaintsService 
{
    @Autowired
    private OaComplaintsMapper oaComplaintsMapper;

    /**
     * 查询投诉工单
     * 
     * @param id 投诉工单主键
     * @return 投诉工单
     */
    @Override
    public OaComplaints selectOaComplaintsById(Long id)
    {
        return oaComplaintsMapper.selectOaComplaintsById(id);
    }

    /**
     * 查询投诉工单列表
     * 
     * @param oaComplaints 投诉工单
     * @return 投诉工单
     */
    @Override
    public List<OaComplaints> selectOaComplaintsList(OaComplaints oaComplaints)
    {
        return oaComplaintsMapper.selectOaComplaintsList(oaComplaints);
    }

    /**
     * 新增投诉工单
     * 
     * @param oaComplaints 投诉工单
     * @return 结果
     */
    @Override
    public int insertOaComplaints(OaComplaints oaComplaints)
    {
        oaComplaints.setCreateTime(DateUtils.getNowDate());
        return oaComplaintsMapper.insertOaComplaints(oaComplaints);
    }

    /**
     * 受理工单
     *
     * @param complaintsDTO
     * @return
     */
    @Override
    public int applyComplaints(ComplaintsDTO complaintsDTO) {
        //根据ID查询工单
        OaComplaints oaComplaints = this.oaComplaintsMapper.selectOaComplaintsById(complaintsDTO.getId());
        if (oaComplaints == null) {
            throw new ServiceException("工单不存在");
        }
        if (!oaComplaints.getComplaintsState().equals(PmsConstants.OA_COMPLAINTS_STATE_APPLY)) {
            throw new ServiceException("当前工单不是待受理状态");
        }
        oaComplaints.setComplaintsState(PmsConstants.OA_COMPLAINTS_STATE_REPAIRING);
        oaComplaints.setAppContent(complaintsDTO.getContent());
        oaComplaints.setAssigneeUser(complaintsDTO.getAssigneeUser());
        oaComplaints.setAssigneeUserName(complaintsDTO.getAssigneeUserName());
        //受理时间
        oaComplaints.setAppTime(new Date());
        return this.oaComplaintsMapper.updateOaComplaints(oaComplaints);
    }

    /**
     * 处理投诉
     *
     * @param complaintsDTO
     * @return
     */
    @Override
    public int handleComplaints(ComplaintsDTO complaintsDTO) {
        //根据ID查询工单
        OaComplaints oaComplaints = this.oaComplaintsMapper.selectOaComplaintsById(complaintsDTO.getId());
        if (oaComplaints == null) {
            throw new ServiceException("工单不存在");
        }
        if (!oaComplaints.getComplaintsState().equals(PmsConstants.OA_COMPLAINTS_STATE_REPAIRING)) {
            throw new ServiceException("当前工单不是处理中状态");
        }
        oaComplaints.setComplaintsState(PmsConstants.OA_COMPLAINTS_STATE_SCORE);
        oaComplaints.setConfirmContent(complaintsDTO.getContent());
        return this.oaComplaintsMapper.updateOaComplaints(oaComplaints);
    }

    /**
     * 客户评价投诉工单
     *
     * @param complaintsDTO
     * @return
     */
    @Override
    public int doComplaintsScore(ComplaintsDTO complaintsDTO) {
        // //根据ID查询工单
        OaComplaints oaComplaints = this.oaComplaintsMapper.selectOaComplaintsById(complaintsDTO.getId());
        if (oaComplaints == null) {
            throw new ServiceException("工单不存在");
        }
        if (!oaComplaints.getComplaintsState().equals(PmsConstants.OA_COMPLAINTS_STATE_SCORE)) {
            throw new ServiceException("当前工单不是待待评价状态");
        }
        oaComplaints.setComplaintsState(PmsConstants.OA_COMPLAINTS_STATE_COMPLETE);
        oaComplaints.setOwnerScore(complaintsDTO.getOwnerScore());
        oaComplaints.setOwnerScoreRemark(complaintsDTO.getOwnerScoreRemark());
        Date applyTime = oaComplaints.getApplyTime();
        oaComplaints.setTimeConsum(DateUtil.between(applyTime, DateUtil.date(), DateUnit.HOUR));
        oaComplaints.setEndTime(new Date());
        return this.oaComplaintsMapper.updateOaComplaints(oaComplaints);
    }

    /**
     * 提交物业回访
     *
     * @param complaintsDTO
     * @return
     */
    @Override
    public int doComplaintsVisit(ComplaintsDTO complaintsDTO) {
        // //根据ID查询工单
        OaComplaints oaComplaints = this.oaComplaintsMapper.selectOaComplaintsById(complaintsDTO.getId());
        if(oaComplaints==null){
            throw new ServiceException("工单不存在");
        }
        if(!oaComplaints.getComplaintsState().equals(PmsConstants.OA_COMPLAINTS_STATE_COMPLETE)){
            throw new ServiceException("当前工单不是已完成状态");
        }
        oaComplaints.setReturnState(PmsConstants.OA_RETURN_STATE_HAS_VISIT);
        oaComplaints.setReturnResult(complaintsDTO.getReturnResult());
        oaComplaints.setReturnRemark(complaintsDTO.getReturnRemark());
        return this.oaComplaintsMapper.updateOaComplaints(oaComplaints);
    }

    /**
     * 撤销工单
     *
     * @param id
     * @return
     */
    @Override
    public int doCancel(Long id) {
        // //根据ID查询工单
        OaComplaints oaComplaints = this.oaComplaintsMapper.selectOaComplaintsById(id);
        if(oaComplaints==null){
            throw new ServiceException("工单不存在");
        }
        if(oaComplaints.getComplaintsState().equals(PmsConstants.OA_COMPLAINTS_STATE_SCORE)){
            throw new ServiceException("当前工单是待评价状态,不能撤销");
        }
        if(oaComplaints.getComplaintsState().equals(PmsConstants.OA_COMPLAINTS_STATE_COMPLETE)){
            throw new ServiceException("当前工单是完成状态,不能撤销");
        }
        if(oaComplaints.getComplaintsState().equals(PmsConstants.OA_COMPLAINTS_STATE_CANCEL)){
            throw new ServiceException("当前工单已撤销,不能重复撤销");
        }
        oaComplaints.setComplaintsState(PmsConstants.OA_COMPLAINTS_STATE_CANCEL);
        return this.oaComplaintsMapper.updateOaComplaints(oaComplaints);
    }
}
